package com.qf.servlet;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author zdl
 * @date 2025/1/16
 */
@WebServlet("/list")
public class ListEmpServlet extends HttpServlet {
    private EmpService empService = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String queryName = req.getParameter("queryName");
        System.out.println("搜索框拿到的name:" + queryName);
        String querySex = req.getParameter("querySex");
        System.out.println("搜索框拿到的sex:" + querySex);
        HttpSession session = req.getSession();
        String name = (String) session.getAttribute("name");
        int pageSize = 5;
        String pageNo = req.getParameter("pageNo");
        if (pageNo == null) {
            pageNo = "1";
        }
        int pNo = Integer.parseInt(pageNo);
        //查询总条数
        Long empCount = empService.findEmpCount(queryName, querySex);
        //尾页
        int totalPage = 0;
        if (empCount % pageSize == 0) {
            totalPage = (int) (empCount / pageSize);
        } else {
            totalPage = (int) (empCount / pageSize) + 1;
        }
        PrintWriter out = resp.getWriter();
        List<Emp> allEmp = empService.findAllEmp((pNo - 1) * pageSize, pageSize, queryName, querySex);
        out.write("<html>");
        out.write("<body>");
        out.write("<div align='center'>");
        //通过session获取username属性值
        out.write("<h1>" + name + "用户登录成功!</h1>");
        out.write("<h2><a href = '/logout'>退出登录</a></h2>");
        out.write("<h3><a href = '/add.jsp'>添加数据</a></h2>");
        out.write("<form action = '/list'>");
        out.write("姓名:<input type = 'text' name = 'queryName'>");
        out.write("性别:<input type = 'radio' name = 'querySex' value = '男'>男<input type = 'radio' name = 'querySex' value = '女'>女");
        out.write("<input type = 'submit' value = '搜索'>");
        out.write("<input type = 'reset' value = '重置'>");
        out.write("</form>");
        out.write("<table border = '2'>");
        out.write("<tr>");
        out.write("<th>编号</th>");
        out.write("<th>姓名</th>");
        out.write("<th>年龄</th>");
        out.write("<th>性别</th>");
        out.write("<th>金钱</th>");
        out.write("<th colspan='2'>操作</th>");
        out.write("</tr>");
        for (Emp emp : allEmp) {
            out.write("<tr>");
            out.write("<td>" + emp.getId() + "</td>");
            out.write("<td>" + emp.getName() + "</td>");
            out.write("<td>" + emp.getAge() + "</td>");
            out.write("<td>" + emp.getSex() + "</td>");
            out.write("<td>" + emp.getSalary() + "</td>");
            out.write("<td><a href='/delete?id=" + emp.getId() + "'>删除</a></td>");
            out.write("<td><a href='/update?id=" + emp.getId() + "'>修改</a></td>");
            out.write("</tr>");
        }
        out.write("</table>");
        out.write("</div>");
        out.write("<div align = 'center'>");
        if (empCount > pageSize) {
            out.write("<a href = '/list?pageNo=1'>首页</a>");
            if (pNo > 1) {
                StringBuilder urlBuilder = new StringBuilder("/list?pageNo=" + (pNo - 1));
                if (queryName != null && !queryName.isEmpty()) {
                    urlBuilder.append("&queryName=").append(queryName);
                }
                if (querySex != null && !querySex.isEmpty()) {
                    urlBuilder.append("&querySex=").append(querySex);
                }
                out.write("<a href='" + urlBuilder.toString() + "'>上一页</a>");
                // out.write("<a href='/list?pageNo=" + (pNo - 1) + "&queryName=" + queryName + "&querySex=" + querySex + "'>上一页</a>");
            }
            if (pNo < totalPage) {
                StringBuilder urlBuilder = new StringBuilder("/list?pageNo=" + (pNo + 1));
                if (queryName != null && !queryName.isEmpty()) {
                    urlBuilder.append("&queryName=").append(queryName);
                }
                if (querySex != null && !querySex.isEmpty()) {
                    urlBuilder.append("&querySex=").append(querySex);
                }
                out.write("<a href='" + urlBuilder.toString() + "'>下一页</a>");
                //out.write("<a href='/list?pageNo=" + (pNo + 1) + "&queryName=" + queryName + "&querySex=" + querySex + "'>下一页</a>");
            }
            out.write("<a href = '/list?pageNo=" + totalPage + "&queryName=" + queryName + "&querySex=" + querySex + "'>尾页</a>");
        }

        out.write("<div>");
        out.write("</body>");
        out.write("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
